package com.medvalley.tcm.controller;

import com.google.common.collect.Maps;
import com.medvalley.tcm.base.Result;
import com.medvalley.tcm.domain.*;
import com.medvalley.tcm.pojo.*;
import com.medvalley.tcm.service.ReportDetailService;
import com.medvalley.tcm.service.ReportService;
import com.medvalley.tcm.service.SMSService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
@Api(value = "报告相关接口")
public class ReportController extends BaseController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private SMSService smsService;

    @Autowired
    private ReportDetailService reportDetailService;

    /**
     * 获取报告列表
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "报告列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result listReport(HttpServletRequest request) {
        String sessionPhone = getSessionPhone(request);
        List<SickInfoBrief> sickAndUsers = reportService.findReportList(sessionPhone);
        return success(sickAndUsers);
    }

    @ApiOperation(value = "报告摘要")
    @RequestMapping(value = "/brief", method = RequestMethod.POST)
    public Result reportBrief(@RequestParam long reportId, HttpServletRequest request) {
        String sessionPhone = getSessionPhone(request);
        SickInfoBrief sickAndUser = reportService.findReportBrief(sessionPhone, reportId);
        return success(sickAndUser);
    }

    @ApiOperation(value = "无报告时，随机养生建议")
    @RequestMapping(value = "/random/yangsheng", method = RequestMethod.POST)
    public Result randomYangSheng() {
        Map<String, Object> advices = Maps.newHashMap();
        List<YangShengXieZi> xieZis = reportService.findXieZiRandom(1);
        List<YangShengSeaon> seasons = reportService.findSeasonRandom(1);
        advices.put("xiezi", xieZis);
        advices.put("seasons", seasons);
        return success(advices);
    }

    @ApiOperation(value = "添加报告-手机")
    @RequestMapping(value = "/bindByPhone", method = RequestMethod.POST)
    public Result bindReportByPhone(@RequestParam String paraPhone, @RequestParam String smsCode, HttpServletRequest request) {
        String sessionPhone = getSessionPhone(request);
        boolean isSmsCodeRight = smsService.validateCode(paraPhone, smsCode);
        if (!isSmsCodeRight) {
            return error(" 短信验证码错误 ");
        }
        reportService.relateReportByPhone(sessionPhone, paraPhone);
        return success();
    }

    @ApiOperation(value = "添加报告-体检码")
    @RequestMapping(value = "/bindByBarCode", method = RequestMethod.POST)
    public Result bindReportByBarCode(@RequestParam String barCode, @RequestParam String pwd, HttpServletRequest request) {
        String sessionPhone = getSessionPhone(request);
        reportService.relateReportByBarCode(sessionPhone, pwd, barCode);
        return success();
    }

    @ApiOperation(value = "报告详情的列表页")
    @RequestMapping(value = "/detail/list", method = RequestMethod.POST)
    public Result gotoReportDetailList(@RequestParam long reportId, HttpServletRequest request) {
        String sessionPhone = getSessionPhone(request);
        if (!reportService.checkUser2Sick(sessionPhone, reportId)) {
            return error("查无此报告");
        }
        return success();
    }

    @ApiOperation(value = "楔子详情")
    @RequestMapping(value = "/xiezi/detail", method = RequestMethod.POST)
    public Result getXieZi(@RequestParam long reportId, HttpServletRequest request) {
        List<YangShengXieZi> yangShengXieZis = reportService.findXieZiBySickId(reportId);
        return success(yangShengXieZis);
    }


    @ApiOperation(value = "运动养生详情")
    @RequestMapping(value = "/sport/detail", method = RequestMethod.POST)
    public Result getSport(@RequestParam long reportId, HttpServletRequest request) {
        List<YangShengSports> sports = reportService.findSportBySickId(reportId);
        return success(sports);
    }

    @ApiOperation(value = "季节养生详情")
    @RequestMapping(value = "/season/detail", method = RequestMethod.POST)
    public Result getSeason(@RequestParam long reportId, HttpServletRequest request) {
        List<YangShengSeaon> yangShengSeaons = reportService.findSeaonBySickId(reportId);
        return success(yangShengSeaons);
    }


    @ApiOperation(value = "茶饮养生详情")
    @RequestMapping(value = "/tea/detail", method = RequestMethod.POST)
    public Result getTea(@RequestParam long reportId, HttpServletRequest request) {
        List<YangShengTea> teas = reportService.findTeaBySickId(reportId);
        return success(teas);
    }


    @ApiOperation(value = "膳食详情")
    @RequestMapping(value = "/eat/detail", method = RequestMethod.POST)
    public Result getEat(@RequestParam long reportId, HttpServletRequest request) {
        List<YangShengEat> eats = reportService.findEatBySickId(reportId);
        return success(eats);
    }

    @ApiOperation(value = "药浴养生详情")
    @RequestMapping(value = "/method/detail", method = RequestMethod.POST)
    public Result getMethod(@RequestParam long reportId, HttpServletRequest request) {
        List<YangShengMethod> methods = reportService.findMethodBySickId(reportId);
        return success(methods);
    }

    @ApiOperation(value = "艾灸养生详情")
    @RequestMapping(value = "/acupuncture/detail", method = RequestMethod.POST)
    public Result getAcupuncture(@RequestParam long reportId, HttpServletRequest request) {
        List<YangShengAcupuncture> yangShengAcupunctures = reportService.findAcupunctureBySickId(reportId);
        return success(yangShengAcupunctures);
    }

    @ApiOperation(value = "健康趋势报告")
    @RequestMapping(value = "/health/detail", method = RequestMethod.POST)
    public Result getHealth(@RequestParam long reportId, HttpServletRequest request) {
        String loginPhone = this.getSessionPhone(request);
        BodySystemPoint bodySystemPoint = reportDetailService.calculateBodySystemPoint(reportId, loginPhone);
        return success(bodySystemPoint);
    }

    @ApiOperation(value = "健康趋势报告-无session")
    @RequestMapping(value = "/health/detail_common", method = RequestMethod.POST)
    public Result getHealthNoSession(@RequestParam long reportId) {
        BodySystemPoint bodySystemPoint = reportDetailService.calculateBodySystemPoint(reportId);
        return success(bodySystemPoint);
    }

    @ApiOperation(value = "体检结果汇总")
    @RequestMapping(value = "/collect/detail", method = RequestMethod.POST)
    public Result getCollect(@RequestParam long reportId, HttpServletRequest request) {
        String loginPhone = this.getSessionPhone(request);
        Map result = reportDetailService.resultCollect(reportId, loginPhone);
        return success(result);
    }

    @ApiOperation(value = "望诊健康评估")
    @RequestMapping(value = "/wangzhen/detail", method = RequestMethod.POST)
    public Result getWangZhen(@RequestParam long reportId, HttpServletRequest request) {
        String loginPhone = this.getSessionPhone(request);
        TonguePojo tonguePojo = reportDetailService.getFaceAndTongue(reportId, loginPhone);
        return success(tonguePojo);
    }
    @ApiOperation(value = "望诊健康评估-无session")
    @RequestMapping(value = "/wangzhen/detail_common", method = RequestMethod.POST)
    public Result getWangZhenNoSession(@RequestParam long reportId) {
        TonguePojo tonguePojo = reportDetailService.getFaceAndTongue(reportId);
        return success(tonguePojo);
    }

    @ApiOperation(value = "脉诊健康评估")
    @RequestMapping(value = "/pulse/detail", method = RequestMethod.POST)
    public Result getPulseDetail(@RequestParam long reportId, HttpServletRequest request) {
        String loginPhone = this.getSessionPhone(request);
        PulsePojo pulsePojo = reportDetailService.getPulseData(reportId, loginPhone);
        return success(pulsePojo);
    }

    @ApiOperation(value = "体质健康评估")
    @RequestMapping(value = "/body/detail", method = RequestMethod.POST)
    public Result getBodyDetail(@RequestParam long reportId, HttpServletRequest request) {
        String loginPhone = this.getSessionPhone(request);
        String getPhysique = reportDetailService.getPhysique(reportId, loginPhone);
        return success("", getPhysique);
    }

    @ApiOperation(value = "耳电健康评估")
    @RequestMapping(value = "/ear/detail", method = RequestMethod.POST)
    public Result getEarDetail(@RequestParam long reportId, HttpServletRequest request) {
        String loginPhone = this.getSessionPhone(request);
        EarPojo earPojo = reportDetailService.getEarData(reportId, loginPhone);
        return success(earPojo);
    }

    @ApiOperation(value = "大数据深度分析")
    @RequestMapping(value = "/bigdata/detail", method = RequestMethod.POST)
    public Result getBigDataDetail(@RequestParam long reportId, HttpServletRequest request) {
        String loginPhone = this.getSessionPhone(request);
        BigDataAnalyze bigDataAnalyze = reportDetailService.getBigDataAnalyze(reportId, loginPhone);
        return success(bigDataAnalyze);
    }

    @ApiOperation(value = "读懂中医报告")
    @RequestMapping(value = "/read/detail", method = RequestMethod.POST)
    public Result getReadReportDetail(@RequestParam long reportId, HttpServletRequest request) {
        ReadReport readReport = new ReadReport();
        return success(readReport);
    }

    @ApiOperation(value = "解析结果")
    @RequestMapping(value = "/analyze/description", method = RequestMethod.POST)
    public Result getAnalyze(@RequestParam String name) {
        String description = reportDetailService.analyzeResult(name);
        return success(description);
    }

}
